package com.meixiang.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.account.AllPrivilegeEntity;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.util.GlideHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的特权
 */
public class AllPrivilegeAdapter extends RecyclerView.Adapter<AllPrivilegeAdapter.MyViewHolder> {

    private List<AllPrivilegeEntity> mAllPrivilegeList = new ArrayList<>();
    private Context context;

    public AllPrivilegeAdapter(Context context) {
        this.context = context;
    }

    /**
     * 刷新数据
     */
    public void refreshData(List<AllPrivilegeEntity> list) {
        this.mAllPrivilegeList.addAll(list);
        this.notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_privilege, parent, false));
        return myViewHolder;
    }

    public OnItemClick onItemClickListener;

    public void setOnItemClickListener(OnItemClick onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            });
        }

        AllPrivilegeEntity allPrivilegeEntity = mAllPrivilegeList.get(position);
        if (allPrivilegeEntity != null) {
            GlideHelper.showImage(context, allPrivilegeEntity.getPhoto(), R.drawable.image_default_white_bg, holder.mIvPicture);
            holder.mTvMsgTitle.setText(allPrivilegeEntity.getName());
            holder.mTvMsgContent.setText(allPrivilegeEntity.getContent());
        }

    }

    @Override
    public int getItemCount() {

        return mAllPrivilegeList.size() > 0 ? mAllPrivilegeList.size() : 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_picture)
        ImageView mIvPicture;
        @Bind(R.id.tv_msg_title)
        TextView mTvMsgTitle;
        @Bind(R.id.tv_msg_content)
        TextView mTvMsgContent;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}